package com.zaofan.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Express;
import com.zaofan.bean.ExpressBuilder;
import com.zaofan.service.ExpressBuilderService;
import com.zaofan.service.ExpressService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

/**
 * 送餐人员管理
 *
 */
@WebServlet("/express")
public class ExpressController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private ExpressService expressService = new ExpressService();
	private ExpressBuilderService expressBuilderService = new ExpressBuilderService();

	/**
	 * 送餐人员列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<Express> expresss = expressService.findAll(params);
			Long count = expressService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", expresss, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * 送餐人员 大楼列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void builders_list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Integer expressId = Integer.valueOf(request.getParameter("id"));
			List<ExpressBuilder> builders = expressBuilderService.findByExpressId(expressId);
			JsonUtils.writeSuccessPageJson("查询成功", builders, 0L, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * 送餐人员 分配大楼
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void builders(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Integer id = Integer.valueOf(request.getParameter("id"));
			String builderIds = request.getParameter("builderIds");
			expressService.updateExpressBuilderInfo(id, builderIds);
			JsonUtils.writeSuccessJson("操作成功", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("操作失败!!", response);
	}

	/**
	 * 增加送餐人员
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Express express = WebUtils.write2bean(request, Express.class);
			expressService.add(express);
			JsonUtils.writeSuccessJson("添加成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("添加失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("添加失败!!", response);
	}

	/**
	 * 更新送餐人员信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Express express = WebUtils.write2bean(request, Express.class);
			expressService.updateExpressInfo(express);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	/**
	 * 获取送餐人员信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			Express express = expressService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", express, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	/**
	 * 设置送餐人员状态
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void state(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Express express = expressService.findById(id);
			express.setState(express.getState() == 0 ? 1 : 0);
			expressService.setExpressState(express);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("更新失败!!", response);
	}

}
